﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Home.Site.Models.Account;

namespace Home.Site.Helpers
{
    public class GravatarHelper
    {
        public static string HashEmailForGravatar(string currentUser)
        {
            var accountRepository = new AccountRepository();
            var user = accountRepository.GetUserByLogin(currentUser);
            if (user != null)
            {
                var email = string.IsNullOrEmpty(user.Email) ? null : user.Email;
                if (email != null)
                {
                    var md5Hasher = MD5.Create();
                    var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(email)); 
                    var sBuilder = new StringBuilder();
                    for (var i = 0; i < data.Length; i++)
                    {
                        sBuilder.Append(data[i].ToString("x2"));
                    }
                    return sBuilder.ToString();
                }
            }
            return null;
        }
    }
}